package csoei.bbs.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csoei.bbs.Dao.UserDao;
import csoei.bbs.beans.User;
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;public login() {super();}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(UserDao.validate(request.getParameter("username"), request.getParameter("password"))){
			User user=new User(UserDao.getUsernameById(request.getParameter("username")),request.getParameter("username"),true);
			HttpSession session=request.getSession();
			session.setAttribute("username", User.getUsername());
			session.setAttribute("uid",user.getUserid());
			response.sendRedirect(request.getHeader("Referer"));
		}else{
			
			response.sendRedirect("index.jsp");
		}
	}
}
